package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class PositionService{

	@Autowired
	private PositionDao positionDao;
	@Autowired
	private PositionAuthorityDao positionAuthorityDao;
	
	private QPosition $ = QPosition.position;
	
	private QPositionAuthority $_ = QPositionAuthority.positionAuthority;
	
	@Autowired
	public void setPositionDao(PositionDao positionDao) {
		this.positionDao = positionDao;
	}
	
	public void setPositionAuthorityDao(PositionAuthorityDao positionAuthorityDao) {
		this.positionAuthorityDao = positionAuthorityDao;
	}

	public List<Position> findAll(){
		return ListUtil.toList(positionDao.findAll());
	}
				
	public List<Position> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(positionDao.findAll(condition));
	}		

	public Page<Position> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return positionDao.findAll(condition, pageRequest);
	}
	
	public Position getPosition(Long id){
		return positionDao.getOne(id);
	}
	
	public void deletePosition(Long id){
		//1.删除岗位权限中的映射
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.position.id.eq(id));
		this.positionAuthorityDao.deleteInBatch(this.positionAuthorityDao.findAll(condition));
		//2.删除岗位
		positionDao.delete(id);
	}

	public void savePosition(Position position) {
		if (null == position.getOrderNumber()) {
			position.setOrderNumber(this.getOrderNumber());
		}
		positionDao.save(position);
	}

	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<Position> positions = this.positionDao.findAll(condition,pageRequest);
		if(positions.getTotalElements() > 0){
			Position position = positions.getContent().get(0);
			return position.getOrderNumber() + 1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		Position currentModule = this.positionDao.findOne(id);
		Position targetModule = this.positionDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.positionDao.save(currentModule);
		this.positionDao.save(targetModule);
		return true;
	}

	public Position findOne(Long id) {
		return this.positionDao.findOne(id);
	}
	
	
	

}
		
